using UnityEngine;
using QFramework;
using System.Collections;
// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class Enemy_MasouMan : EnemyFathers
    {
        public override BoxCollider2D OnOwer => HitHurtBox;
        /// <summary>
        /// 敌人生命值
        /// </summary>
        public float enemyHp = 30;

        /// <summary>
        /// 敌人移动速度
        /// </summary>
        public float enemySpeed = 2;

        private bool mIgnoreHurt = false;

        public override float EnemyHp { get => enemyHp; set { if (value < 0) value = 0; enemyHp = value; } }
        public override float EnemyMoveSpeed { get => enemySpeed; set => enemySpeed = value; }

        public override void Awake()
        {
            base.Awake();

            HitHurtBox.OnTriggerEnter2DEvent(collider =>
            {
                var hurt = collider.GetComponent<HitHurtBox>();
                if (hurt != null)
                {
                    if (hurt.Owner.CompareTag("Player"))
                    {
                        //触发伤害计算
                        var player = hurt.Owner.GetComponent<Player>();
                        if (player != null)
                        {
                            DataManager.Instance.playerModel.PlayerHp.Value -= 1;
                        }
                    }
                }


                //Debug.Log(collider.gameObject.name);
            });
        }

        public override void Start()
        {
            base.Start();
        }

        public override void Die()
        {
            base.Die();
            enemySpeed = 0;
            DataManager.Instance.abilityUpModel.SkillEnemyNumbers.Value += 1;
            StartCoroutine(DieAnimPlaye());
        }

        public override void Update()
        {
            base.Update();
            MoToPlayer();
        }

        public override void MoToPlayer()
        {
            base.MoToPlayer();
            if (!mIgnoreHurt)
            {
                if (Player.Instance)
                {
                    var dic = (Player.Instance.transform.position - transform.position).normalized;

                    FaceToTarget(Player.Instance.transform.position);

                    transform.Translate(dic * EnemyMoveSpeed * Time.deltaTime);
                }
                else
                {
                    transform.Translate(Vector2.zero);
                }
            }
        }

        public override void Hurt(float value, bool force = false, bool critical = false)
        {
            base.Hurt(value, force, critical);

            if (mIgnoreHurt && !force)
                return;
            mIgnoreHurt = true;
            transform.Translate(Vector2.zero);
            FloatingTextController.Play(this.transform.position + Vector3.up * 0.5f, value.ToString("0"), critical);

            ActionKit.Delay(0.2f, () =>
            {
                //AudioKit.PlaySound("Hit");
                this.enemyHp -= value;
                //this.Triangle.color = Color.white;
                mIgnoreHurt = false;
            }).Start(this);

            if (EnemyHp <= 0)
            {
                Die();
            }
        }


        IEnumerator DieAnimPlaye()
        {
            //Debug.Log("执行死亡动画播放");
            transform.Translate(Vector2.zero);
            HitHurtBox.GetComponent<BoxCollider2D>().enabled = false;
            SelfAnimator.Play("Enemy_MasouManDie");
            yield return null;

            var playerTime = SelfAnimator.GetCurrentAnimatorStateInfo(0);
            float waitTime = playerTime.length;
            yield return new WaitForSeconds(waitTime);
            GameManager.Instance.InsExpobj(this.transform);
            this.DestroyGameObjGracefully();
        }

        public override void SetLeftScale()
        {
            base.SetLeftScale();


            //var scale = transform.localScale;
            //scale.x = -Mathf.Abs(scale.x);
            //transform.localScale = scale;

        }
        public override void SetRightScale()
        {
            base.SetRightScale();


            //var scale = transform.localScale;
            //scale.x = Mathf.Abs(scale.x);
            //transform.localScale = scale;

        }
    }
}
